package com.java2007.hotel.service.impl;

import com.java2007.hotel.dao.FoodDao;
import com.java2007.hotel.dao.impl.FoodDaoImpl;
import com.java2007.hotel.entity.DinnerCar;
import com.java2007.hotel.pojo.DinnerTable;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.service.DinnerCarService;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/27
 **/
public class DinnerCarServiceImpl implements DinnerCarService {
    private FoodDao foodDao = new FoodDaoImpl();

    @Override
    public List<DinnerCar> addFoodToDinnerCar(Integer foodId, Integer number, List<DinnerCar> dinnerCarList) throws SQLException {
        //判断购物车内是否存在相同的菜
        // 如果菜在购物车内存在，修改数量
        for (DinnerCar dinnerCar : dinnerCarList) {
            if (dinnerCar.getDetailFoodId().intValue() == foodId.intValue()) {
                /**判断数量是否小于等于0
                 * 如果数量小于等于0，删除
                 * 否则修改购物车内的数据
                 * */
                if (number <= 0) {
                    //删除
                    dinnerCarList.remove(dinnerCar);
                } else {
                    //修改信息
                    dinnerCar.setDetailNumber(dinnerCar.getDetailNumber() + number);
                    dinnerCar.setDetailUpdateTime(new Date());
                    dinnerCar.setDetailTotalPrice(dinnerCar.getDetailPrice() * dinnerCar.getDetailNumber());
                }
                return dinnerCarList;
            }
        }
        //不存在相同的菜
        Food food = foodDao.findByfoodId(foodId);
        DinnerCar dinnerCar = new DinnerCar();
        dinnerCar.setDetailNumber(number);
        dinnerCar.setDetailFoodId(foodId);
        dinnerCar.setDetailFoodName(food.getFoodName());
        dinnerCar.setDetailCreateTime(new Date());
        dinnerCar.setDetailUpdateTime(new Date());
        dinnerCar.setDetailPrice(food.getFoodPrice());
        dinnerCar.setDetailTotalPrice(dinnerCar.getDetailPrice() * dinnerCar.getDetailNumber());
        dinnerCarList.add(dinnerCar);

        return dinnerCarList;
    }



}
